const http = require('../../../../utils/http.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        navBar: {
            title: '我的成绩',
            btn: 'goBack'
        },
        animation: ''
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 页面初始进入分数动画
     */
    scoreChange() {
        let that = this,
            i = 0,
            score = that.data.score,
            addScore = setInterval(function () {
                i++
                if (i >= score) {
                    clearInterval(addScore)
                    return
                }
                that.setData({
                    score: i
                })
            }, 800 / score),

            animation = wx.createAnimation({
                duration: 800,
                timingFunction: 'linear',
                delay: 0,
                success: function (res) {
                    // console.log(res)
                }
            })

        this.animation = animation

        animation.rotate(score * 3.6).step()

        this.setData({
            animation: animation.export()
        })
    },

    /**
     * 到错题本
     */
    goWrong() {
        let ltid = this.data.ltid;
        console.log(ltid)
        wx.navigateTo({
            url: '/pages/account/dar/score/wrong/wrong?ltid=' + ltid,
        })
    },

    /**
     * 到开始测验
     */
    goStart() {
        let id = this.data.id
        wx.navigateTo({
            // url: '/pages/account/dar/score/start/start',
            url: '/pages/course/test/test?id=' + id,
        })
    },

    /**
     * 到测试记录
     */
    goRecord() {
        let ltid = this.data.ltid
        wx.navigateTo({
            url: '/pages/account/dar/score/record/record?ltid=' + ltid,
        })
    },

    /**
     * 获取我的测评成绩
     */
    getMytestInfo(id) {
        let that = this,
            url = 'Test_mytestInfo',
            token = wx.getStorageSync('token')

        http._post(
            url,
            { token: token, mytid: id },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    console.log(data.data.ltid)
                    that.setData({
                        score: Number(data.data.max_score),
                        ltid: data.data.ltid,
                        mytid: data.data.mytid,
                        animation: ''
                    })
                    that.scoreChange()
                } else {
                    that.setData({
                        score: 0
                    })
                }
            },
            err => {
                console.log(err)
            }
        )

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let mytid = options.mytid,
            id = options.id
        this.setData({
            id: id
        })
        this.getMytestInfo(mytid)  // 获取成绩
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})